// use/entities/user.entity.ts
import { Column, Entity, PrimaryGeneratedColumn, BeforeInsert, OneToOne, ManyToOne, OneToMany } from 'typeorm';
import * as bcrypt from 'bcryptjs';
import { Role } from '../../role/entities/role.entity'; // Assuming you have a Role entity
import { Dept } from 'src/modules/system/dept/entities/dept.entity';
@Entity('sys_user')
export class User {
  @PrimaryGeneratedColumn('uuid')
  Id: string;
 
  @Column({ length: 100 })
  userName: string; // 用户名
 
  @Column({ length: 100 })
  password: string;  // 密码

  @Column({ length: 100, nullable: true })
  realName: string;  //真实姓名

  @Column({ length: 100, nullable: true })
  phone: string;  // 手机号

  @Column({ length: 100, nullable: true })
  avatar: string;  // 头像

  @ManyToOne(() => Dept, (Dept) => Dept.Users)
  Dept: Dept; // 关联角色实体

  @ManyToOne(() => Role, (Role) => Role.Users)
  Role: Role; // 关联角色实体
  
  @Column()
  status: number; // 用户状态，1为正常，0为禁用

  @Column({default: 0})
  deleted: number; // 删除标志，1为未删除，0为已删除

  @Column()
  tenantId: string; // 租户ID
 
  @Column({
    type: 'timestamp',
    default: () => 'CURRENT_TIMESTAMP',
  })
  createTime: Date;
 
  @Column({
    type: 'timestamp',
    default: () => 'CURRENT_TIMESTAMP',
    onUpdate: 'CURRENT_TIMESTAMP',
  })
  updateTime: Date;
  
  @BeforeInsert() 
  async encryptPwd() { 
    this.password = await bcrypt.hashSync(this.password); 
  } 

  @Column()
  createBy: string;

  @Column()
  updateBy: string;
  
}